package com.southwind.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.southwind.common.CarPageModel;
import com.southwind.common.ResultObj;
import com.southwind.entity.Car;
import com.southwind.entity.User;
import com.southwind.mapper.CarMapper;
import com.southwind.service.CarService;
import com.southwind.service.UserService;
import com.southwind.vo.CarVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2024-07-04
 */
@RestController
@RequestMapping("/car")
public class CarController {

    @Autowired
    private CarService carService;
    @Autowired
    private UserService userService;
    @Autowired
    private CarMapper carMapper;

    @GetMapping("/list")
    public Map list(CarPageModel carPageModel){
        Page<Car> page = new Page<>(carPageModel.getPage(), carPageModel.getLimit());
        QueryWrapper<Car> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(carPageModel.getUsername())){
            QueryWrapper<User> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.like("username", carPageModel.getUsername());
            List<User> userList = this.userService.list(queryWrapper1);
            List<Integer> userIdList = new ArrayList<>();
            for (User user : userList) {
                userIdList.add(user.getId());
            }
            queryWrapper.like(StringUtils.isNotBlank(carPageModel.getCarnumber()), "number", carPageModel.getCarnumber());
            queryWrapper.in(userIdList.size() > 0,"uid",userIdList);
        } else {
            queryWrapper.like(StringUtils.isNotBlank(carPageModel.getCarnumber()), "number", carPageModel.getCarnumber());
        }
        Page<Car> resultPage = this.carService.page(page, queryWrapper);
        Map map = new HashMap();
        map.put("count", resultPage.getTotal());
        List<CarVO> list = new ArrayList<>();
        for (Car record : resultPage.getRecords()) {
            CarVO carVO = new CarVO();
            BeanUtils.copyProperties(record, carVO);
            carVO.setUsername(this.userService.getById(record.getUid()).getUsername());
            list.add(carVO);
        }
        map.put("data", list);
        return map;
    }

    @GetMapping("/show")
    public List<Car> show(){
        return this.carService.list();
    }

    @GetMapping("/showWork")
    public List<Car> showWork(){
        return this.carMapper.listForWork();
    }

    @GetMapping("/user")
    public List<Car> user(){
        return this.carMapper.listForUser();
    }

    @GetMapping("/userEdit/{id}")
    public List<Car> userEdit(@PathVariable("id") Integer id){
        List<Car> cars = this.carMapper.listForUser();
        Car car = this.carService.getById(id);
        cars.add(car);
        return cars;
    }

    @GetMapping("/list2")
    public Map list2(CarPageModel carPageModel){
        Page<Car> page = new Page<>(carPageModel.getPage(), carPageModel.getLimit());
        QueryWrapper<Car> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(carPageModel.getCarnumber()), "number", carPageModel.getCarnumber());
        Page<Car> resultPage = this.carService.page(page, queryWrapper);
        Map map = new HashMap();
        map.put("count", resultPage.getTotal());
        List<CarVO> list = new ArrayList<>();
        for (Car record : resultPage.getRecords()) {
            CarVO carVO = new CarVO();
            BeanUtils.copyProperties(record, carVO);
            carVO.setUsername(this.userService.getById(record.getUid()).getUsername());
            list.add(carVO);
        }
        map.put("data", list);
        return map;
    }

    @PostMapping("/add")
    public ResultObj add(@RequestBody Car car){
        boolean save = this.carService.save(car);
        if(save) return ResultObj.CAR_ADD_SUCCESS;
        return ResultObj.CAR_ADD_ERROR;
    }

    @PostMapping("/update")
    public ResultObj update(@RequestBody Car car){
        boolean updateById = this.carService.updateById(car);
        if(updateById) return ResultObj.CAR_UPDATE_SUCCESS;
        return ResultObj.CAR_UPDATE_ERROR;
    }

    @DeleteMapping("/delete/{id}")
    public ResultObj delete(@PathVariable("id") Integer id){
        boolean removeById = this.carService.removeById(id);
        if(removeById) return ResultObj.CAR_DELETE_SUCCESS;
        return ResultObj.CAR_DELETE_ERROR;
    }


}

